package com.taokatao.cardmee.business.cardcrud;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.taokatao.cardmee.C;
import com.taokatao.cardmee.Utils;


public class GradientColorView extends View {

    public static final int MODE_NORMAL = 0;
    public static final int MODE_CLIP = 1;

    int mGradientMode = MODE_NORMAL;
    float mRadius;
    int mBackgroundColor;
    int mOriginColor, mNewColor;
    float mAnimPercent;
    ValueAnimator animator;
    final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    public GradientColorView(Context context) {
        this(context, null);
    }

    public GradientColorView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    public void setBackgroundColor(int color) {
        mBackgroundColor = color;
        mOriginColor = mBackgroundColor;
        postInvalidate();
    }

    public void dynamicChangeColor(int newColor) {
        if (animator != null) animator.cancel();
        animator = ValueAnimator.ofFloat(0, 1);
        animator.setDuration(C.settings.ANIMATION_TIME);

        mNewColor = newColor;
        mOriginColor = mBackgroundColor;

        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mAnimPercent = (float) animation.getAnimatedValue();
                mBackgroundColor = ce.evaluate(mAnimPercent, mOriginColor, mNewColor);

                invalidate();
            }
        });
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                mAnimPercent = 0;
                mBackgroundColor = mNewColor;
                mOriginColor = mBackgroundColor;

                invalidate();
            }
        });
        animator.start();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (animator != null) animator.cancel();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mRadius = (float) Math.sqrt(w * w + h * h);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mGradientMode == MODE_NORMAL) {
            canvas.drawColor(mBackgroundColor);
        } else if (mGradientMode == MODE_CLIP) {
            canvas.drawColor(mOriginColor);
            paint.setColor(mNewColor);
            canvas.drawCircle(20, 20, mRadius * mAnimPercent, paint);
        }
    }

    public void setDynamicChangeMode(int mode) {
        mGradientMode = mode;
    }

    private final TypeEvaluator<Integer> ce = new TypeEvaluator<Integer>() {
        @Override
        public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
            int startColor = startValue;
            int endColor = endValue;

            int alpha = (int) (Color.alpha(startColor) + fraction * (Color.alpha(endColor) - Color.alpha(startColor)));
            int red = (int) (Color.red(startColor) + fraction * (Color.red(endColor) - Color.red(startColor)));
            int green = (int) (Color.green(startColor) + fraction * (Color.green(endColor) - Color.green(startColor)));
            int blue = (int) (Color.blue(startColor) + fraction * (Color.blue(endColor) - Color.blue(startColor)));

            return Color.argb(alpha, red, green, blue);
        }
    };

}
